import { useState, useMemo } from 'react';

const useModalVisible = (defaultValue = {}) => {
  const [visible, setVisible] = useState(() => defaultValue);

  const actions = useMemo(() => {
    const setTrue = modalNames => {
      if (Array.isArray(modalNames)) {
        const obj = {};
        modalNames.forEach(modalName => {
          obj[modalName] = true;
        });
        setVisible({
          ...visible,
          ...obj,
        });
      } else {
        setVisible({
          ...visible,
          [modalNames]: true,
        });
      }
    };
    const setFalse = modalNames => {
      if (Array.isArray(modalNames)) {
        const obj = {};
        modalNames.forEach(modalName => {
          obj[modalName] = false;
        });
        setVisible({
          ...visible,
          ...obj,
        });
      } else {
        setVisible({
          ...visible,
          [modalNames]: false,
        });
      }
    };

    return {
      setTrue,
      setFalse,
    };
  }, [visible]);

  return [visible, actions];
};

export default useModalVisible;
